package winter_vacation_homework.day_15;

import java.util.Stack;

/**
 *  【牛客题号: CC142 反转数字】
 *  https://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a?tpId=46&&tqId=29171&rp=1&ru=/ta/classic-code&qru=/ta/classic-code/question-ranking
 */
public class InversionOfDigital {
    public int reverse (int x) {
        // write code here
        int num1 = 0;
        while (x != 0){
            int num2 = x % 10;
            //比前几位都小时最后一位不影响相等时判断最后一个就好了这想法是真他么的巧妙啊
            if (num1 < Integer.MIN_VALUE / 10 || (num1 == Integer.MIN_VALUE / 10 && x < -8)){
                return 0;
            }
            if (num1 > Integer.MAX_VALUE / 10 || (num1 == Integer.MAX_VALUE / 10 && x > 7)){
                return 0;
            }
            num1 = num1 * 10 + num2;
            x /= 10;
        }
        return num1;

    }
}
